package com.ruoyi.quartz.service.impl;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.utils.bean.DateRanger;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.domain.SysJobLogFetcher;
import com.ruoyi.quartz.domain.SysJobLogTable;
import com.ruoyi.quartz.domain.dto.SysJobLogListInput;
import com.ruoyi.quartz.service.ISysJobLogService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.sql.JSqlClient;
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysJobLogServiceImpl implements ISysJobLogService {
    private final JSqlClient sqlClient;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @param dateRanger 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLogListInput jobLog, DateRanger dateRanger) {
        SysJobLogTable table = SysJobLogTable.$;
        return sqlClient.createQuery(table)
                .where(
                        table.jobName().eqIf(jobLog.getJobName()),
                        table.jobGroup().eqIf(jobLog.getJobGroup()),
                        table.status().eqIf(jobLog.getStatus()),
                        table.createTime().betweenIf(DateRanger.isAllNotEmpty(dateRanger), dateRanger.getBeginTime(), dateRanger.getEndTime())
                )
                .select(table.fetch(SysJobLogFetcher.$.allScalarFields()))
                .execute();
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(long jobLogId) {
        return sqlClient.findById(SysJobLog.class, jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        sqlClient.saveCommand(jobLog)
                .setMode(SaveMode.INSERT_ONLY)
                .execute();
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(long[] logIds) {
        return sqlClient.deleteByIds(SysJobLog.class,
                        Arrays.stream(logIds)
                        .boxed().toList())
                .getTotalAffectedRowCount();
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(long jobId) {
        return sqlClient.deleteById(SysJobLog.class,jobId)
                .getTotalAffectedRowCount();
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        SysJobLogTable table = SysJobLogTable.$;
        sqlClient.createDelete(table).execute();
    }

    @Override
    public Page<SysJobLog> selectJobLogPage(SysJobLogListInput jobLog, DateRanger dateRanger, PageQuery pageQuery) {
        SysJobLogTable table = SysJobLogTable.$;
        return sqlClient.createQuery(table)
                .where(
                        table.jobName().eqIf(jobLog.getJobName()),
                        table.jobGroup().eqIf(jobLog.getJobGroup()),
                        table.status().eqIf(jobLog.getStatus()),
                        table.createTime().betweenIf(DateRanger.isAllNotEmpty(dateRanger), dateRanger.getBeginTime(), dateRanger.getEndTime())
                )
                .select(table.fetch(SysJobLogFetcher.$.allScalarFields()))
                .fetchPage(pageQuery.getPageNum(), pageQuery.getPageSize());
    }
}
